import javax.swing.*;
import java.awt.*;

public class AnswerLabel extends JLabel {

    boolean abort, point_used;

    public static final int SAFE = 0, EXPLICT = 1, ABORT = 2;

    int status;

    void abort() {
        setStatus(ABORT);
    }

    void setStatus(int s) {
        status = s;
    }

    int getStatus() {return status;}

    void reset() {
        setStatus(ABORT);
        point_used = false;
    }

    void loadDouble(double d) {
        //System.out.println("loadDouble(" + d + ")");
        this.setText(d + "");
        String s = this.getText();
        if(s.charAt(s.length() - 1) == '0' && s.charAt(s.length() - 2) == '.') this.setText(s.substring(0, s.length() - 2));
    }

    void append(char c) {
        //System.out.println("append(" + c + ")");
        if(getStatus() != SAFE) {
            setStatus(SAFE);
            this.setText("0");
        }
        /*
        if(c == '.' && point_used == true) return;
        if(this.getText() != "0") this.setText(this.getText() + c);
        else this.setText("" + c);
        if(c == '.') point_used = true;
        */
        if(c == '.') {
            if(point_used == true) return;
            point_used = true;
            this.setText(this.getText() + c);
            return;
        }

        if(this.getText() != "0") this.setText(this.getText() + c);
        else this.setText("" + c);
    }

    void backspace() {
        if(getStatus() != SAFE) {
            setStatus(SAFE);
            this.setText("0");
        }else{
            String s = getText();
            char c = s.charAt(s.length() - 1);
            if(c == '.') point_used = false;
            setText(s.substring(0, s.length() - 1));
        }
    }

    public AnswerLabel()
    {
        reset();
        this.setText("0");
        this.setFont(new Font("MS Yahei", Font.PLAIN, 20));
    }
}
